RAIS  3.2
C:/Projekte/RAIS/common/Query.cs
Go to the documentation of this file.
00001 
00028 using System;
00029 using System.Collections.Generic;
00030 using System.Data;
00031 using System.Text;
00032 
00033 using QueryTypes = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Query.QueryTypeData>;
00034 using QueryParameters = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.QueryParameter>;
00035 
00036 namespace RAIS.Common.TableManagement
00037 {
00038         public class Query: DBObject
00039         {
00040                 //*********************************************************************
00041                 #region Enumerators
00042                 public enum CustomizationType
00043                 {
00044                         ListWithFilter,
00045                         QueryReport,
00046                         StatisticsReport,
00047                         ConsistencyCheck,
00048                         EntryFilter,
00049             RanAutogeneration
00050                 }
00051                 public enum QueryType
00052                 {
00053                         ConsistencyCheck,
00054                         EntryFilter,
00055                         PreselectionFilter,
00056                         PreselectionList,
00057                         Other,
00058                         Query,
00059                         Statistics,
00060             RanAutogeneration,
00061             DataRoleRestriction
00062                 }
00063                 #endregion
00064                 //*********************************************************************
00065                 #region Fields
00066                 static QueryTypes queryTypesDictionary;
00067 
00068                 QueryType type;
00069                 string queryText;
00070                 bool compiled = false;
00071                 QueryParameters queryParameters = new QueryParameters();
00072                 List<string> newParameterNames = new List<string>();
00073                 #endregion
00074                 //*********************************************************************
00075                 #region Properties
00076                 public static QueryTypes QueryTypesDictionary
00077                 {
00078                         get { return Query.queryTypesDictionary; }
00079                         set { Query.queryTypesDictionary = value; }
00080                 }
00081 
00082                 public string QueryText
00083                 {
00084                         get { return queryText; }
00085             set
00086             {
00087                 queryText = value;
00088 
00089                 List<string> queryVariables = GetVariablesFromQueryText(queryText);
00090                 GetParametersFromQueryText(queryText, queryVariables);
00091             }
00092                 }
00093                 public bool Compiled
00094                 {
00095                         get { return this.compiled; }
00096                         set { this.compiled = value; }
00097                 }
00098                 public QueryType Type
00099                 {
00100                         get { return type; }
00101                         set { type = value; }
00102                 }
00103                 public QueryTypeData TypeData
00104                 {
00105                         get { return queryTypesDictionary[this.TypeText]; }
00106                 }
00107                 public string TypeText
00108                 {
00109                         get
00110                         {
00111                                 string result = string.Empty;
00112                                 foreach (KeyValuePair<string, QueryTypeData> queryTypeItem in queryTypesDictionary)
00113                                         if (queryTypeItem.Value.Type == this.type)
00114                                         {
00115                                                 result = queryTypeItem.Value.VisibleName;
00116                                                 break;
00117                                         }
00118                                 return result;
00119                         }
00120                         set
00121                         {
00122                                 this.type = queryTypesDictionary[value].Type;
00123                         }
00124                 }
00125                 public bool QueryForFilters
00126                 {
00127                         get
00128                         {
00129                                 bool queryForFilters = false;
00130                                 if (this.type == QueryType.PreselectionFilter
00131                                         || this.type == QueryType.PreselectionList
00132                                         || this.type == QueryType.Query
00133                                         || this.type == QueryType.Statistics
00134                                 )
00135                                 {
00136                                         queryForFilters = true;
00137                                 }
00138                                 return queryForFilters;
00139                         }
00140                 }
00141                 public QueryParameters QueryParameters
00142                 {
00143                         get { return queryParameters; }
00144                         set { queryParameters = value; }
00145                 }
00146                 public List<string> NewParameterNames
00147                 {
00148                         get { return newParameterNames; }
00149                         set { newParameterNames = value; }
00150                 }
00151                 public string NewParameterNamesInXml
00152                 {
00153                         get
00154                         {
00155                                 // Create new QueryParameter to get the ID for default QueryParameterType
00156                                 QueryParameter queryParameter = new QueryParameter();
00157 
00158                                 StringBuilder xmlString = new StringBuilder("'<Parameters>");
00159                                 if (this.NewParameterNames.Count != 0)
00160                                 {
00161                                         int order = 1;
00162                                         foreach (string parameter in this.NewParameterNames)
00163                                                 xmlString.AppendFormat("<Parameter Name=\"{0}\" TypeID=\"{1}\" Order=\"{2}\" VisibleName=\"{3}\" />"
00164                                                         , parameter, queryParameter.TypeData.ID, order++, queryParameter.VisibleName);
00165                                 }
00166 
00167                                 xmlString.Append("</Parameters>'");
00168 
00169                                 return xmlString.ToString();
00170                         }
00171                 }
00172                 public string QueryNameWithParameters
00173                 {
00174                         get
00175                         {
00176                                 StringBuilder queryName = new StringBuilder(this.Name);
00177 
00178                                 if (this.type == QueryType.ConsistencyCheck
00179                                         || this.type == QueryType.EntryFilter
00180                     || this.type == QueryType.RanAutogeneration)
00181                                 {
00182 
00183                                         queryName.Append("(");
00184                                         if (this.QueryParameters != null && this.QueryParameters.Count != 0)
00185                                         {
00186                                                 bool firstParameter = true;
00187                                                 foreach (KeyValuePair<string, QueryParameter> kvp in this.QueryParameters)
00188                                                 {
00189                                                         QueryParameter queryParameter = kvp.Value;
00190 
00191                                                         if (!firstParameter)
00192                                                                 queryName.Append(", ");
00193 
00194                                                         queryName.Append(queryParameter.VisibleName);
00195                                                         firstParameter = false;
00196                                                 }
00197                                         }
00198                                         queryName.Append(")");
00199                                 }
00200                                 return queryName.ToString();
00201                         }
00202                 }
00203                 public bool ParametersChanged
00204                 {
00205                         get
00206                         {
00207                                 bool changed = false;
00208                                 foreach (string parameterName in this.NewParameterNames)
00209                                 {
00210                                         if (!this.queryParameters.ContainsKey(parameterName))
00211                                         {
00212                                                 changed = true;
00213                                                 break;
00214                                         }
00215                                 }
00216 
00217                                 if (!changed)
00218                                 {
00219                                         foreach (KeyValuePair<string, QueryParameter> kvp in this.QueryParameters)
00220                                         {
00221                                                 if (!this.NewParameterNames.Contains(kvp.Key))
00222                                                 {
00223                                                         changed = true;
00224                                                         break;
00225                                                 }
00226                                         }
00227                                 }
00228 
00229                                 return changed;
00230                         }
00231                 }
00232                 #endregion
00233                 //*********************************************************************
00234                 #region Constructors
00235                 public Query()
00236                 { }
00237 
00238                 public Query(IDataRecord record)
00239                 {
00240                         this.id = (int)record["PK Query ID"];
00241                         this.name = record["Query Name"].ToString();
00242                         this.queryText = record["Query Text"].ToString();
00243                         this.compiled = ((byte)record["Compiled"] == 0) ? false : true;
00244                         this.type = (QueryType)Enum.Parse(typeof(QueryType), record["Query Type Name"].ToString());
00245                 }
00246                 #endregion 
00247                 //*********************************************************************
00248                 #region Static Methods
00249                 static public QueryTypeData GetQueryTypeDataByQueryType(QueryType type)
00250                 {
00251                         QueryTypeData result = null;
00252                         foreach (KeyValuePair<string, QueryTypeData> queryTypeItem in queryTypesDictionary)
00253                                 if (queryTypeItem.Value.Type == type)
00254                                 {
00255                                         result = queryTypeItem.Value;
00256                                         break;
00257                                 }
00258                         return result;
00259                 }
00260 
00261                 static public QueryType GetQueryTypeByCustomizationType(CustomizationType customizationType)
00262                 {
00263                         QueryType queryType = QueryType.PreselectionList;
00264 
00265                         switch (customizationType)
00266                         {
00267                                 case Query.CustomizationType.ConsistencyCheck:
00268                                         queryType = Query.QueryType.ConsistencyCheck;
00269                                         break;
00270                                 case Query.CustomizationType.EntryFilter:
00271                                         queryType = Query.QueryType.EntryFilter;
00272                                         break;
00273                                 case Query.CustomizationType.ListWithFilter:
00274                                         queryType = Query.QueryType.PreselectionList;
00275                                         break;
00276                                 case Query.CustomizationType.QueryReport:
00277                                         queryType = Query.QueryType.Query;
00278                                         break;
00279                                 case Query.CustomizationType.StatisticsReport:
00280                                         queryType = Query.QueryType.Statistics;
00281                                         break;
00282                                 case Query.CustomizationType.RanAutogeneration:
00283                                         queryType = Query.QueryType.RanAutogeneration;
00284                                         break;
00285                         }
00286                         return queryType;
00287                 }
00288                 #endregion
00289                 //*********************************************************************
00290                 #region Private Methods
00291                 List<string> GetVariablesFromQueryText(string queryText)
00292                 {
00293                         List<string> queryVariables = new List<string>();
00294 
00295                         string[] linesArray = this.QueryText.Split(new char[] { '\n' });
00296                         foreach (string line in linesArray)
00297                         {
00298                                 if (line.Contains("DECLARE"))
00299                                 {
00300                                         List<string> variables = GetAtMarkedNamesFromString(line);
00301 
00302                                         queryVariables.AddRange(variables);
00303                                 }
00304                         }
00305                         return queryVariables;
00306                 }
00307 
00308                 void GetParametersFromQueryText(string queryText, List<string> queryVariables)
00309                 {
00310                         this.newParameterNames.Clear();
00311 
00312                         string[] tempArray = queryText.Split(new string[] { "@@" }, StringSplitOptions.RemoveEmptyEntries);
00313                         foreach (string text in tempArray)
00314                         {
00315                                 List<string> atMarkedNames = GetAtMarkedNamesFromString(text);
00316                                 foreach (string atMarkedName in atMarkedNames)
00317                                 {
00318                                         if (!queryVariables.Contains(atMarkedName)
00319                                                 && !this.newParameterNames.Contains(atMarkedName))
00320                                         {
00321                                                 this.newParameterNames.Add(atMarkedName);
00322                                         }
00323                                 }
00324                         }
00325                 }
00326 
00327                 List<string> GetAtMarkedNamesFromString(string text)
00328                 {
00329                         List<string> atMarkedNames = new List<string>();
00330                         string[] tempArray = text.Split(new char[] { '@' });
00331                         if (tempArray.Length > 1)
00332                         {
00333                                 for (int i = 1; i < tempArray.Length; i++)
00334                                 {
00335                                         int firstNonLetterOrDigitPosition = 0;
00336                                         for (int n = 0; n < tempArray[i].Length; n++)
00337                                         {
00338                                                 if (!Char.IsLetterOrDigit(tempArray[i], n))
00339                                                 {
00340                                                         firstNonLetterOrDigitPosition = n;
00341                                                         break;
00342                                                 }
00343                                                 else
00344                                                 {
00345                                                         if (n == tempArray[i].Length - 1)
00346                                                                 firstNonLetterOrDigitPosition = n + 1;
00347                                                 }
00348                                         }
00349 
00350                                         if (firstNonLetterOrDigitPosition != 0)
00351                                         {
00352                                                 string atMarkedName = tempArray[i].Substring(0, firstNonLetterOrDigitPosition);
00353                                                 if (!atMarkedNames.Contains(atMarkedName))
00354                                                         atMarkedNames.Add(atMarkedName);
00355                                         }
00356                                 }
00357                         }
00358                         return atMarkedNames;
00359                 }
00360                 #endregion
00361                 //*********************************************************************
00362                 #region Nested classes
00363                 public class QueryTypeData : DBTypeObject
00364                 {
00365                         //*********************************************************************
00366                         #region Properties
00367                         public QueryType Type
00368                         {
00369                                 get { return (QueryType)Enum.Parse(typeof(QueryType), this.name); }
00370                         }
00371                         #endregion
00372                         //*********************************************************************
00373                         #region Constructors
00374                         public QueryTypeData()
00375                         { }
00376 
00377                         public QueryTypeData(IDataRecord record)
00378                         {
00379                                 this.id = (int)record["PK Query Type ID"];
00380                                 this.name = record["Query Type Name"].ToString();
00381                                 this.visibleName = record["Query Type Visible Name"].ToString();
00382                         }
00383                         #endregion
00384                 }
00385                 #endregion
00386         }
00387 }